{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20041a5c-1676-4158-b354-69a0f6751181",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0           1           2          3          4          5       \\\n",
      "0    track_id        type  traveled_d  avg_speed        lat        lon   \n",
      "1           1         Car       48.85   9.770344  37.977391  23.737688   \n",
      "2           2  Motorcycle       98.09  19.839417  37.977642  23.737400   \n",
      "3           3  Motorcycle       63.80  18.228752  37.977997  23.737264   \n",
      "4           4  Motorcycle      145.72  26.229014  37.978135  23.737072   \n",
      "..        ...         ...         ...        ...        ...        ...   \n",
      "918       918         Car       78.83  30.846243  37.980629  23.735083   \n",
      "919       919  Motorcycle       19.50   9.234518  37.979327  23.735628   \n",
      "920       920         Car       48.97  24.486209  37.978413  23.735528   \n",
      "921       921  Motorcycle       46.68  30.007124  37.980020  23.736861   \n",
      "922       922         Car       35.00  23.335533  37.978428  23.735538   \n",
      "\n",
      "      6        7        8           9       ... 122821 122822 122823 122824  \\\n",
      "0      speed  lon_acc  lat_acc        time  ...   None   None   None   None   \n",
      "1     4.9178   0.0518  -0.0299    0.000000  ...   None   None   None   None   \n",
      "2    16.9759  -0.0361  -0.0228    0.000000  ...   None   None   None   None   \n",
      "3    20.1906  -0.0795  -0.3395    0.000000  ...   None   None   None   None   \n",
      "4     2.7555  -0.0302   0.0948    0.000000  ...   None   None   None   None   \n",
      "..       ...      ...      ...         ...  ...    ...    ...    ...    ...   \n",
      "918  38.2160   0.0372  -0.0533  809.600000  ...   None   None   None   None   \n",
      "919   2.9344   0.0011  -0.0237  811.200000  ...   None   None   None   None   \n",
      "920  22.8316  -0.0250  -0.0808  811.600000  ...   None   None   None   None   \n",
      "921  32.3581  -0.0493  -0.1050  813.200000  ...   None   None   None   None   \n",
      "922  24.1909  -0.0077  -0.0660  813.400000  ...   None   None   None   None   \n",
      "\n",
      "    122825 122826 122827 122828 122829 122830  \n",
      "0     None   None   None   None   None   None  \n",
      "1     None   None   None   None   None   None  \n",
      "2     None   None   None   None   None   None  \n",
      "3     None   None   None   None   None   None  \n",
      "4     None   None   None   None   None   None  \n",
      "..     ...    ...    ...    ...    ...    ...  \n",
      "918   None   None   None   None   None   None  \n",
      "919   None   None   None   None   None   None  \n",
      "920   None   None   None   None   None   None  \n",
      "921   None   None   None   None   None   None  \n",
      "922   None   None   None   None   None   None  \n",
      "\n",
      "[923 rows x 122831 columns]\n"
     ]
    }
   ],
   "source": [
    "# %load acceleration difference.py\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import math\n",
    "\n",
    "# 读取 CSV 文件\n",
    "df = pd.read_csv(\"20181024_d1_0830_0900.csv\", encoding=\"utf-8\", header=None)\n",
    "\n",
    "# 使用 ';' 分隔列数据并转换为多个列\n",
    "df_split = df[0].str.split('; ', expand=True)\n",
    "print(df_split)\n",
    "\n",
    "x = []\n",
    "xavg=[]\n",
    "y = []\n",
    "yavg=[]\n",
    "xaccleration = []\n",
    "yaccleration = []\n",
    "accleration = []\n",
    "zhengfu = []\n",
    "feature_values = []\n",
    "speed1 = []\n",
    "acclerationdif = []\n",
    "time=[]\n",
    "transform=[]\n",
    "time1=[]\n",
    "\n",
    "# 从第二行起提取数据\n",
    "for i in range(1, len(df_split)):  # 从第二行开始\n",
    "    for j in range(0, (len(df_split.columns) -4)// 6):  # 根据数据每6列一个周期提取数据\n",
    "        # 确保x和y是数值类型\n",
    "        if pd.isnull(df_split.iloc[i, 6 * j + 4]) or (df_split.iloc[i, 6 * j + 4]==''):\n",
    "            break\n",
    "        try:\n",
    "            y_value = float(df_split.iloc[i, 6 * j + 4])  # 尝试将y值转换为float\n",
    "            y.append(y_value)\n",
    "        except ValueError:\n",
    "            print(i)\n",
    "            print(j)\n",
    "            print(df_split.iloc[i, 6 * j + 4])\n",
    "        x.append(float(df_split.iloc[i, 6 * j + 5]))  # 获取x坐标并转换为float\n",
    "        time.append(float(df_split.iloc[i, 6 * j + 9]))  # 获取x坐标并转换为float\n",
    "        # 将加速度值转换为float类型\n",
    "        xaccleration.append(float(df_split.iloc[i, 6 * j + 7]))  \n",
    "        yaccleration.append(float(df_split.iloc[i, 6 * j + 8]))  \n",
    "        # 将速度值转换为float类型\n",
    "        speed1.append(float(df_split.iloc[i, 6 * j + 6]))  # 获取特征值\n",
    "    transform.append(len(x))\n",
    "\n",
    "# 计算加速度\n",
    "accleration = list(map(lambda x, y: math.sqrt(x**2 + y**2), xaccleration, yaccleration))\n",
    "\n",
    "# 判断速度的变化趋势\n",
    "for i in range(0, len(speed1) - 1):\n",
    "    if speed1[i] <= speed1[i + 1]:\n",
    "        zhengfu.append(1)\n",
    "    else:\n",
    "        zhengfu.append(-1)\n",
    "zhengfu.append(zhengfu[len(zhengfu) - 2])  # 最后一项与倒数第二项相同\n",
    "\n",
    "# 计算特征值\n",
    "feature_values = list(map(lambda x, y: x * y, accleration, zhengfu))\n",
    "for i in range(0, len(feature_values) - 1):\n",
    "    if i+1 in transform:\n",
    "        continue\n",
    "    acclerationdif.append(feature_values[i+1] -feature_values[i])\n",
    "for i in range(0, len(x) - 1):\n",
    "    if i+1 in transform:\n",
    "        continue\n",
    "    xavg.append((x[i]+x[i+1])/2)\n",
    "for i in range(0, len(y) - 1):\n",
    "    if i+1 in transform:\n",
    "        continue\n",
    "    yavg.append((y[i]+y[i+1])/2)\n",
    "for i in range(0, len(time) - 1):\n",
    "    if i+1 in transform:\n",
    "        continue\n",
    "    time1.append(time[i+1]-time[i])\n",
    "acclerationdif = list(map(lambda x, y: math.sqrt(x**2 + y**2), acclerationdif, time1))\n",
    "\n",
    "# 绘制散点图\n",
    "plt.figure(figsize=(10, 6))  # 设置图形大小\n",
    "plt.scatter(xavg, yavg, c=acclerationdif, cmap='viridis', s=np.array(acclerationdif) / 10)  # 除以10来缩放点的大小\n",
    "plt.xlabel('X')\n",
    "plt.ylabel('Y')\n",
    "plt.title('Scatter Plot with Feature Values')\n",
    "\n",
    "# 设置坐标轴刻度显示间隔\n",
    "plt.xticks(np.arange(min(xavg), max(xavg), (max(xavg) - min(xavg)) / 5))  # x轴每隔一定范围显示一个刻度\n",
    "plt.yticks(np.arange(min(yavg), max(yavg), (max(yavg) - min(yavg)) / 5))  # y轴每隔一定范围显示一个刻度\n",
    "\n",
    "# 旋转坐标轴标签，避免重叠\n",
    "plt.xticks(rotation=45)  # 旋转x轴标签45度\n",
    "plt.yticks(rotation=45)  # 旋转y轴标签45度\n",
    "\n",
    "# 添加网格线\n",
    "plt.grid(True)\n",
    "\n",
    "# 添加色标\n",
    "plt.colorbar(label='Feature Value')  # 添加色标\n",
    "\n",
    "# 显示图形\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "525e6aee-25fd-44d3-9ec9-ba6ffd208203",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
